<template>
  <div class="v-element-tree-select g-jc-sb">
    <div
      v-if="mode === 'horizontal'"
      class="horizontal"
    >
      <div
        v-for="(item, index) of valueData"
        :key="index"
        class="value-item"
        @click="handleClickItem(item)"
        @contextmenu="propertyModelContextMenu($event, item)"
      >
        <span>
          <img
            style="margin-right: 4px; height: 16px"
            :src="item.icon"
          />
          <span v-if="item.name">{{ locale.getTByKey(`modelConfig.${item.name}`, item.name) }}</span>
        </span>
      </div>
      <div
        v-if="selected && !row.readonly && !tableElementDisabled"
        class="add-btn"
      >
        <img
          src="statics/icons/app/iconmore.png"
          class="g-pointer"
          @click="handleAdd"
        />
      </div>
    </div>
    <PropertyNodeLabel
      v-else
      class="_list_scroll"
      :row="row"
      :selected="selected"
      :selectedModelId="selectedModelId"
      @clickItem="handleClickItem"
    />

    <div
      v-if="selected && !row.readonly && !isModelDisabled && mode !== 'horizontal'"
      style="border-left: 1px solid #ddd; line-height: 14px"
      class="g-pd-4 g-flex-0"
    >
      <div>
        <img
          src="statics/icons/app/iconadd.png"
          class="g-pointer"
          @click="handleAdd"
        />
      </div>
      <div class="g-m-t-4">
        <img
          src="statics/icons/app/iconreduce.png"
          class="g-pointer"
          @click="handleRemove"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropertyRow, ModelValue } from "../../types";
import { ref, Ref, computed, watch, inject } from "vue";
import { BusEvent } from "@/constants/Enums";
import { propertyUtil } from "../../PropertyUtil";
import { modelTreeSelectWindow } from "@/modules/model/modals/modelTreeSelect";
import { modelService } from "@/modules/model/service";
import PropertyNodeLabel from "../custom/PropertyNodeLabel.vue";
import { propertyModelContextMenu } from "../propertyUtil";
import { locale } from "@/util/locale";

const isModelDisabled = inject("isModelDisabled", ref(false)) as Ref<boolean>;

/**
 * 模型多选-树选择弹窗
 */
const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
  tableElementDisabled?: boolean;
  mode?: string; // 新增横向horizontal模式, 以便在一行内显示内容
}>();

const valueData = computed(() => {
  return (props.row.value || []) as ModelValue[];
});

const selectedModelId = ref("");

watch(
  () => props.selected,
  val => {
    if (val === false) {
      selectedModelId.value = "";
    }
  }
);

const handleAdd = () => {
  const row = props.row;
  const checkedIds = (row.value as ModelValue[]).map(item => item.id);
  const delay = true;
  modelTreeSelectWindow.popup(
    {
      loadMethod: "getElementsAndStereotypes",
      loadParams: JSON.stringify({
        row: { ...row },
        projectId: app.activeProject!.config.id
      }),
      checkedIds,
      checkable: true,
      attrTip: "ElementType",
      title: "选择元素类型",
      delay
    },
    async data => {
      if (!data.success || !delay) return;
      const ids = data.selectedId as string[];
      const dto = propertyUtil.getUpdatePropertyDtoByPropertyRow(row, app.activeProject!.config.id);
      dto.value = ids;
      const result = await modelService.updateModelProperty(dto);
      if (result.code === 1000) {
        app.$bus.emit(BusEvent.REFRESH_PROPERTY, row.modelId, row.key);
      }
      modelTreeSelectWindow.done();
    }
  );
};

function handleRemove() {
  if (!selectedModelId.value) {
    app.$notify({
      title: "提示",
      type: "error",
      message: "请选中元素再操作移除"
    });
    return;
  }
  propertyUtil.removePropertyAndRefresh(props.row, selectedModelId.value);
}

function handleClickItem(item: { id: string }) {
  selectedModelId.value = item.id;
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/theme.scss";
.v-element-tree-select {
  // height: 100%;
  ._selected {
    color: $theme-color;
  }
  ._list_scroll {
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 8px;
      height: 8px;
    }
  }
  .horizontal {
    height: 24px;
    width: 100%;
    padding-right: 30px;
    padding-left: 11px;
    position: relative;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 24px;
    .value-item {
      display: inline-block;
      height: 16px;
      margin-right: 10px;
      line-height: 16px;
      img {
        margin-right: 6px;
        line-height: 16px;
        vertical-align: middle;
      }
      span {
        line-height: 14px;
        vertical-align: middle;
      }
    }
    .add-btn {
      line-height: 1;
      position: absolute;
      top: 50%;
      right: 5px;
      transform: translateY(-50%);
    }
  }
}
</style>
